package edu.itla.medifacil.persistence.managers;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import edu.itla.medifacil.persistence.entities.Speciality;
import edu.itla.medifacil.persistence.entities.Suffering;

public class SpecialityManager {
	private EntityManager em = null;
	
	public SpecialityManager(){
		this.em = Persistence.createEntityManagerFactory("MediFacil").createEntityManager();
	}
	
	public Speciality save( Map<String, String> parameters ){
		Speciality speciality = new Speciality();
		
		if( parameters.get("id") != null && parameters.get("id").trim().length() > 0 )
			speciality = this.find( Integer.parseInt( parameters.get("id") ) );
		
		speciality.setDescription( parameters.get("description") );
		
		em.getTransaction().begin();
		em.persist(speciality);
		em.getTransaction().commit();
		
		return speciality;
	}
	
	public Speciality find( int specialityId ){
		em.clear();
		return em.find( Speciality.class, specialityId );
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Speciality> findAll(){
		return (List<Speciality>) em.createQuery( "SELECT s FROM Speciality s").getResultList();
	}
}
